×
☰ See All Chapters

Handling textbox and textarea in Selenium WebDriver

Data for “textbox” or “textarea” HTML elements can be entered with the “sendKeys()” method.

Example:  

driver.findElement(By.id("fullname")).sendKeys("Manu Manjunatha");

Special keys, such as Backspace, Enter, Tab, or Shift, require using a special “enum” class of WebDriver, named “Keys”.

driver.findElement(By.id("fullname")).sendKeys(Keys.chord(Keys.SHIFT,"Manu Manjunatha"));

The above command enters the characters in upper case.  chord Simulate pressing many keys at once in a "chord".

You can write the script and test these using our Test Page

selenium-webdriver-textbox-0
 

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

import org.junit.Assert;

 

public class Example {

 

        public static void main(String[] args) {

 

                // configure chromedriver

                System.setProperty("webdriver.chrome.driver", "F:\\My_Programs\\Selenium\\ChromeDriver\\chromedriver.exe");

 

                WebDriver driver = new ChromeDriver();

               

                // Launch website

                driver.get("https://www.tools4testing.com/contents/selenium/testpages/registration-form-testpage");

                       

                //Enter data in Editboxes

                driver.findElement(By.id("fullname")).sendKeys("Manu Manjunatha");

 

                //Clear data in Editboxes

                driver.findElement(By.id("fullname")).clear();

               

                //chord Simulate pressing many keys at once in a "chord".

                driver.findElement(By.id("fullname")).sendKeys(Keys.chord(Keys.SHIFT,"Manu Manjunatha")); //This enters the characters in upper case

               

                //Check entered data is correct

                String text = driver.findElement(By.id("fullname")).getAttribute("value");

                Assert.assertEquals("MANU MANJUNATHA", text);

               

               

                System.out.println("-------------------------------DONE----------------------------------");

                //wait some time before closing

                try {

                        Thread.sleep(7000);

                } catch (InterruptedException ie) {

                }

               

                //close the driver

                driver.quit();

       

        }

}

 

 


All Chapters
Author